/*
* Copyright (c) 2012 Kyle Hayward
* 
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* 
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
* 
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
#pragma once

namespace OptixDotNet 
{
	ref class Acceleration;

	//we've got to forward declare these because of the stupid xml doc.
	ref class GeometryGroup;
	ref class Selector;
	ref class Transform;

	/// <summary>
	/// Contains a set of nodes arranged in a hierarchy. The nodes compile a graph structure that is used by OptiX during ray traversal. <br/>
	/// The nodes can be: <see cref="OptixDotNet::Group">Group</see>, <see cref="OptixDotNet::GeometryGroup">GeometryGroup</see>, 
	/// <see cref="OptixDotNet::Selector">Selector</see>, or <see cref="OptixDotNet::Transform">Transform</see>.
    /// </summary>
	public ref class Group : public ContainerNode, IContainerNode<ContainerNode^>, INodeCollectionProvider<ContainerNode^>
	{
	public:

		/// <summary>
		/// Creates a new Group node.
		/// </summary>
		Group( Context^ context );

		/// <summary>
		/// Checks to make sure the object is valid and consistent
        /// </summary>
		virtual void Validate() override
		{
			CheckError( rtGroupValidate( mGroup ) );
		}

		/// <summary>
		/// Destroys the internal Optix resource
        /// </summary>
		virtual void Destroy() override
		{
			if( mGroup )
				CheckError( rtGroupDestroy( mGroup ) );

			mGroup = 0;
		}

		/// <summary>
		/// Gets the NodeCollection containded by the Group
		/// NodeCollection is comprised of nodes of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property NodeCollection<ContainerNode^>^ Children
		{
			virtual NodeCollection<ContainerNode^>^ get() = INodeCollectionProvider<ContainerNode^>::Collection::get
			{
				return mCollection;
			}
		}

		/// <summary>
		/// Sets the number of child nodes to be attached to the geometry group.
        /// </summary>
		property unsigned int ChildCount
		{
			virtual unsigned int get() = IContainerNode<ContainerNode^>::ChildCount::get
			{
				unsigned int count;
				CheckError( rtGroupGetChildCount( mGroup, &count ) );
				
				return count;
			}
			virtual void set( unsigned int count ) = IContainerNode<ContainerNode^>::ChildCount::set
			{
				CheckError( rtGroupSetChildCount( mGroup, count ) );
			}
		}

		/// <summary>
		/// Gets the child node object attached to a group node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		ContainerNode^ GetChild( unsigned int index );

		/// <summary>
		/// Attaches a child node object to a group node at [index].
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="index">Index of the node</param>
		/// <param name="node">node to attach</param>
		void SetChild( unsigned int index, ContainerNode^ node );

		/// <summary>
		/// Attaches a child node object to the end of a Group node child list
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		/// <param name="node">node to add</param>
		void AddChild( ContainerNode^ node )
		{
			SetChild( ChildCount++, node );
		}

		/// <summary>
		/// Sets the acceleratoin structure for a geometry group
        /// </summary>
		property Acceleration^ Acceleration
		{
			OptixDotNet::Acceleration^ get();
			void set( OptixDotNet::Acceleration^ accel );
		}

	internal:
		Group( Context^ context, RTgroup group );
		inline RTgroup InternalPtr() { return mGroup; }
		virtual RTobject ObjectPtr() override { return InternalPtr(); }

		/// <summary>
		/// Gets or Attaches a child node to a group at [index]
		/// A node object can be one of: Group, Selector, GeometryGroup, or Transform
        /// </summary>
		property ContainerNode^ ChildNodes[unsigned int]
		{
			virtual ContainerNode^ get ( unsigned int index ) = IContainerNode<ContainerNode^>::ChildNodes::get;
			virtual void set ( unsigned int index, ContainerNode^ node ) = IContainerNode<ContainerNode^>::ChildNodes::set;
		}

	private:

		RTgroup	mGroup;
		NodeCollection<ContainerNode^>^ mCollection;
	};
}